
package com.yimixian.app.goods;

import com.yimixian.app.R;
import com.yimixian.app.util.WeChatUtils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.greenrobot.event.EventBus;

public class ShareGoodView extends FrameLayout implements View.OnClickListener {
    public static final String SHAREVIEW_CANCEL_MASKING = "shareview_cancel_masking";

    @InjectView(R.id.tv_share_friend_group)
    TextView mTvShareFriendGroup;
    @InjectView(R.id.tv_share_wechat_friend)
    TextView mTvShareWechatFriend;
    @InjectView(R.id.tv_cancel)
    TextView mTvCancel;
    private String mShareName = "";
    private String mShareURL = "";
    private String mShareStr = "";
    private String mIconUrl = "";
    private String mShareContent="";

    public ShareGoodView(Context context) {
        super(context);
        initGoodsView();
    }

    public ShareGoodView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initGoodsView();
    }

    public ShareGoodView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initGoodsView();
    }

    private void initGoodsView() {
        LayoutInflater.from(getContext()).inflate(R.layout.share_good_detail, this);
        ButterKnife.inject(this);
        mTvCancel.setOnClickListener(this);
        mTvShareFriendGroup.setOnClickListener(this);
        mTvShareWechatFriend.setOnClickListener(this);
    }

    public void bind(String shareName, String shareURL,String iconUrl,String shareContent) {
        mShareName = shareName;
        mShareURL = shareURL;
        mIconUrl = iconUrl;
        mShareContent = shareContent;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                EventBus.getDefault().post(new String(SHAREVIEW_CANCEL_MASKING));
                closeShareViewWithAnim();
                break;
            case R.id.tv_share_friend_group:
                EventBus.getDefault().post(new String(SHAREVIEW_CANCEL_MASKING));
                WeChatUtils.getInstance().share2weixin(mShareURL, mShareName, mShareContent,mIconUrl,
                        WeChatUtils.ShareType.WECHAT_CIRCLE);
                closeShareViewWithAnim();
                break;
            case R.id.tv_share_wechat_friend:
                EventBus.getDefault().post(new String(SHAREVIEW_CANCEL_MASKING));
                WeChatUtils.getInstance().share2weixin(mShareURL, mShareName, mShareContent,mIconUrl,
                        WeChatUtils.ShareType.WECHAT_FRIEND);
                closeShareViewWithAnim();
                break;
        }
    }

    public void closeShareViewWithAnim() {
        Animation animation = AnimationUtils.loadAnimation(getContext(),
                R.anim.socialize_shareboard_animation_out);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                ShareGoodView.this.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        this.startAnimation(animation);
    }

    public void openShareViewWithAnim() {
        ShareGoodView.this.setVisibility(View.VISIBLE);
        Animation animation = AnimationUtils.loadAnimation(getContext(),
                R.anim.socialize_shareboard_animation_in);
        this.startAnimation(animation);
    }

}
